package com.tky.leave.ui;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.TabHost;

import com.tky.leave.R;
import com.tky.leave.ui.fragment.AuditFragment;
import com.tky.leave.ui.fragment.LaunchFragment;

/**
 * Created by WX on 2016/4/27.
 */
public class HomePageActivity extends BaseActivity {

    private FragmentTabHost mTabHost;
    private RadioGroup mTabRg;

    private final Class[] fragments = {LaunchFragment.class, AuditFragment.class};

    @Override
    public void initView() {
        setContentView(R.layout.activity_homepage);

        initActionBar("我发起的", false, false, R.mipmap.ic_launcher);
    }

    @Override
    public void initData() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        // 得到fragment个数
        int count = fragments.length;
        for (int i = 0; i < count; i++) {
            // 为每一个按钮设置图标和文字
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(i + "").setIndicator(i + "");
            mTabHost.addTab(tabSpec, fragments[i], null);
        }

        mTabRg = (RadioGroup) findViewById(R.id.tab_rg_menu);
        mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.tab_rb_1:
                        initActionBar("我发起的", false, false, R.mipmap.ic_launcher);
                        mTabHost.setCurrentTab(0);
                        break;
                    case R.id.tab_rb_2:
                        initActionBar("我审批的", false, false, R.mipmap.ic_launcher);
                        mTabHost.setCurrentTab(1);
                        break;
                    default:
                        break;
                }
            }
        });
        mTabHost.setCurrentTab(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        if (item.getItemId() == R.id.action_edit) {
            return true;
        }
        if (item.getItemId() == R.id.action_share) {
            return true;
        }
        if (item.getItemId() == R.id.action_settings) {
            startActivity(new Intent(this, WriteActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}